<?php
namespace app\home\controller;
use \think\Validate;
use \think\captcha\Captcha;
use think\Session;

class Order extends Common
{
    public function order()
    {
        $user_id = session("user_id");
        $order_list1 = db("order")->where("user_id={$user_id} and statu=0")->order("addtime desc")->select();   //待付款
        //echo "<pre>";
        //var_dump($order_list1);
        foreach($order_list1 as &$val){
            $jsonStr = $val['jsonStr'];
            $json_arr = json_decode($jsonStr,true);
            $new_arr = [];
            foreach($json_arr as $key => $vals){
                $temp_arr = [];
                $food_info = db('food')->where("food_id = '{$key}'")->find();
                $food_name = $food_info['name'];
                $temp_arr[0] = $food_name;
                $temp_arr[1] = $vals;
                array_push($new_arr,$temp_arr);
            }
            $val['food_list'] = $new_arr;
        }
        //var_dump($order_list1);die;

        $order_list2 = db("order")->where("user_id={$user_id} and statu=1")->order("addtime desc")->select();   //待发货
        foreach($order_list2 as &$val){
            $jsonStr = $val['jsonStr'];
            $json_arr = json_decode($jsonStr,true);
            $new_arr = [];
            foreach($json_arr as $key => $vals){
                $temp_arr = [];
                $food_info = db('food')->where("food_id = '{$key}'")->find();
                $food_name = $food_info['name'];
                $temp_arr[0] = $food_name;
                $temp_arr[1] = $vals;
                array_push($new_arr,$temp_arr);
            }
            $val['food_list'] = $new_arr;
        }
        $order_list3 = db("order")->where("user_id={$user_id} and statu=2")->order("addtime desc")->select();   //待收货
        foreach($order_list3 as &$val){
            $jsonStr = $val['jsonStr'];
            $json_arr = json_decode($jsonStr,true);
            $new_arr = [];
            foreach($json_arr as $key => $vals){
                $temp_arr = [];
                $food_info = db('food')->where("food_id = '{$key}'")->find();
                $food_name = $food_info['name'];
                $temp_arr[0] = $food_name;
                $temp_arr[1] = $vals;
                array_push($new_arr,$temp_arr);
            }
            $val['food_list'] = $new_arr;
        }

        $order_list4 = db("order")->where("user_id={$user_id} and statu=3")->order("addtime desc")->select();   //被取消
        foreach($order_list4 as &$val){
            $jsonStr = $val['jsonStr'];
            $json_arr = json_decode($jsonStr,true);
            $new_arr = [];
            foreach($json_arr as $key => $vals){
                $temp_arr = [];
                $food_info = db('food')->where("food_id = '{$key}'")->find();
                $food_name = $food_info['name'];
                $temp_arr[0] = $food_name;
                $temp_arr[1] = $vals;
                array_push($new_arr,$temp_arr);
            }
            $val['food_list'] = $new_arr;
        }

        $order_list5 = db("order")->where("user_id={$user_id} and statu=4")->order("addtime desc")->select();   //已完成
        foreach($order_list5 as &$val){
            $jsonStr = $val['jsonStr'];
            $json_arr = json_decode($jsonStr,true);
            $new_arr = [];
            foreach($json_arr as $key => $vals){
                $temp_arr = [];
                $food_info = db('food')->where("food_id = '{$key}'")->find();
                $food_name = $food_info['name'];
                $temp_arr[0] = $food_name;
                $temp_arr[1] = $vals;
                array_push($new_arr,$temp_arr);
            }
            $val['food_list'] = $new_arr;
        }

        $order_list6 = db("order")->where("user_id={$user_id} and statu=5")->order("addtime desc")->select();   //已完成
        foreach($order_list6 as &$val){
            $jsonStr = $val['jsonStr'];
            $json_arr = json_decode($jsonStr,true);
            $new_arr = [];
            foreach($json_arr as $key => $vals){
                $temp_arr = [];
                $food_info = db('food')->where("food_id = '{$key}'")->find();
                $food_name = $food_info['name'];
                $temp_arr[0] = $food_name;
                $temp_arr[1] = $vals;
                array_push($new_arr,$temp_arr);
            }
            $val['food_list'] = $new_arr;
        }
        $this->assign("typeaa","order");
        $this->assign("user_id",$user_id);
        $this->assign("order_list1",$order_list1);
        $this->assign("order_list2",$order_list2);
        $this->assign("order_list3",$order_list3);
        $this->assign("order_list4",$order_list4);
        $this->assign("order_list5",$order_list5);
        $this->assign("order_list6",$order_list6);
    	return view('order');
    }

    public function setorder()
    {
        $user_id = session("user_id");
        $order_id = $_GET['order_id'];
        $order_info = db('order')->where("id = {$order_id}")->find();
        $jsonStr = $order_info['jsonStr'];
        $json_arr = json_decode($jsonStr,true);
        $new_arr = [];
        foreach($json_arr as $key => $val){
            $temp_arr = [];
            $food_info = db('food')->where("food_id = '{$key}'")->find();
            $food_name = $food_info['name'];
            $temp_arr[0] = $food_name;
            $temp_arr[1] = $val;
            array_push($new_arr,$temp_arr);
        }
        $address_id = $order_info['address_id'];
        $address_content = db('address')->where("id = '{$address_id}'")->value("content");
        if(!$address_content){
            $address_content = "请设置收货地址";
            $this->assign("is_address",0);
        }else{
            $this->assign("is_address",1);
        }
        $order_info['food_list'] = $new_arr;
        $order_info['address_content'] = $address_content;
        // echo "<pre>";
        // var_dump($order_info);die;
        $this->assign("order_info",$order_info);
        $this->assign("typeaa","order");
        $this->assign("user_id",$user_id);
    	return view('setorder');
    }

    public function createOrder(){
        $user_id = session("user_id");
        $address_info = db("address")->where("user_id ='{$user_id}' and is_default=1")->find();
        $address_id = $address_info['id'];
        $sumprice = $_POST['sum'];
        $jsonStr = $_POST['jsonStr'];
        $order_str = "food".time();
        $should_paytime = time()+60*3;
        $data = [
            'user_id'   => $user_id,
            'statu'     => 5,
            'addtime'   => time(),
            'should_paytime' => $should_paytime,
            'jsonStr'   => $jsonStr,
            'sumprice'  => $sumprice,
            'order_str' => $order_str,
            'address_id'=> $address_id
        ];
        $order_id = db("order")->insertGetId($data);
        if($order_id){
            return ['status'=>1,'order_id'=>$order_id];
        }
            return ['status'=>0];
    }

    public function orderPay(){
        $order_id = $_POST['order_id'];
        if(isset($_POST['eatWay'])){
            $eatWay = $_POST['eatWay'];
            $data = [
                'statu'         => 1,
                'paytime'       => time(),
                'eatWay'        => $eatWay,
                'address_id'    => 1
            ];
        }else{
            $data = [
                'statu'         => 1,
                'paytime'       => time(),
                'address_id'    => 1
            ];
        }
        $ret = db("order")->where("id={$order_id}")->update($data);
        if($ret){
            return ['status'=>1];
        }
            return ['status'=>0];
    }

    public function paysuccess(){
    	return view('paysuccess');
    }

    public function delOrder(){
        $order_id = $_POST['order_id'];
        $ret = db("order")->where("id ={$order_id}")->update(array("statu"=>3));
        if($ret){
            return ['status'=>1];
        }
            return ['status'=>0];
    }

    public function confirmGet(){
        $order_id = $_POST['order_id'];
        $ret = db("order")->where("id ={$order_id}")->update(array("statu"=>4));
        if($ret){
            return ['status'=>1];
        }
            return ['status'=>0];
    }
}
